<?php

namespace app\api\controller;

use app\admin\model\Useragentwechat;
use app\admin\model\Userweixin;
use app\common\controller\Api;
use app\common\controller\Frontend;
use app\common\model\WechatAutoreply;
use app\common\model\WechatContext;
use app\common\model\WechatResponse;
use app\common\model\WechatConfig;
use EasyWeChat\Factory;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Payment\Order;
use addons\wechat\library\Wechat as WechatService;
use addons\wechat\library\Config as ConfigService;
use fast\Random;
use think\Db;
use think\Exception;
use think\Hook;
use think\Log;

/**
 * 微信授权
 */
class Wechat extends Api
{
    public $app = null;

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->app = Factory::officialAccount(ConfigService::load());
    }

    public function api()
    {
        $this->app->server->push(function ($message) {

           error_log("======message=========" . json_encode($message));
            switch ($message['MsgType']) {
                case 'event':

                    if ($message['Event'] == 'subscribe') {

                        return "您好欢迎关注手电智购 <a href ='https://bs.50xue.com/index/index/account'>账号推送绑定</a>";
                    }else if($message['Event'] == 'CLICK'){
                        if($message['EventKey'] == '58c7d908c4570'){
                            return '客服电话: 4001158520';
                        }
                    }
                    break;
                case 'text':
                    $content = $message['Content'];
                    if($content == '账号绑定'){

                        return "<a href ='https://sd.api.xfy66.com/index/index/bind'>点击绑定</a>";
                    }
                    return '';
                    break;
                case 'image':
                    return '收到图片消息';
                    break;
                case 'voice':
                    return '收到语音消息';
                    break;
                case 'video':
                    return '收到视频消息';
                    break;
                case 'location':
                    return '收到坐标消息';
                    break;
                case 'link':
                    return '收到链接消息';
                    break;
                case 'file':
                    return '收到文件消息';
                // ... 其它消息
                default:
                    return '收到其它消息';
                    break;
            }
        });
        $response = $this->app->server->serve();
        $response->send();

    }


    /**
     * 登录授权
     */
    public function oauth()
    {

        error_log("=========oauth=========");
        $response = $this->app->oauth->scopes(['snsapi_userinfo'])
            ->redirect();
        $response->send();

    }


    /**
     * 授权回调
     */
    public function callback()
    {
        $user = $this->app->oauth->user();


        $name = $user->name;
        $id = $user->getId();

        $nickname = $user->nickname;
        $avatar = $user->avatar;
        $original = $user->original;

//        $wechatModel = new Useragentwechat();
//        $result = $wechatModel->where("openid", $id)->find();
        $url = $this->request->domain() ."/index/index/account";
        error_log($name."============openid======".$id);
        error_log("============url======".$url);

        $target_url = $url."?name=".$nickname."&openid=".$id;
        error_log($name."============targetUrl======".$target_url);
        header("location:" . $target_url);
        exit();

    }


}
